package com.cn.onetrip.untility;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Untility {

	public static Rotate3D lQuest1Animation;
	public static Rotate3D lQuest2Animation;
	public static Rotate3D rQuest1Animation;
	public static Rotate3D rQuest2Animation;

	static public void initAnimation(int mCenterX, int mCenterY) {
		// DisplayMetrics dm = new DisplayMetrics();
		// dm = getResources().getDisplayMetrics();
		// mCenterX = dm.widthPixels / 2;
		// mCenterY = dm.heightPixels / 2;

		int duration = 1000;
		lQuest1Animation = new Rotate3D(0, 90, 0, mCenterY);
		lQuest1Animation.setFillAfter(true);
		lQuest1Animation.setDuration(duration);

		lQuest2Animation = new Rotate3D(-90, 0, mCenterX, mCenterY);
		lQuest2Animation.setFillAfter(true);
		lQuest2Animation.setDuration(duration);

		rQuest1Animation = new Rotate3D(0, -90, mCenterX, mCenterY);
		rQuest1Animation.setFillAfter(true);
		rQuest1Animation.setDuration(duration);

		rQuest2Animation = new Rotate3D(90, 0, 0, mCenterY);
		rQuest2Animation.setFillAfter(true);
		rQuest2Animation.setDuration(duration);
	}

	public static String getVersionName(Context context) {
		PackageInfo packInfo = null;
		try {
			PackageManager packageManager = context.getPackageManager();
			packInfo = packageManager.getPackageInfo(context.getPackageName(),
					0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		String version = packInfo.versionName;
		return version;
	}

	public void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}
}
